package io.github.slideconflictdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by codeczx on 2018/4/10.
 */

public class HorizontalScrollViewEx extends ViewGroup {

    private static final String TAG = "HorizontalScrollViewEx";
    private float interceptLastX;
    private float interceptLastY;
    private float lastX;
    private float lastY;

    public HorizontalScrollViewEx(Context context) {
        super(context);
    }

    public HorizontalScrollViewEx(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HorizontalScrollViewEx(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        float x = event.getRawX();
        Log.i(TAG, "onInterceptTouchEvent: x:" + x);
        float y = event.getRawY();
        boolean intercept = false;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "onInterceptTouchEvent: down");
                interceptLastX = x;
                interceptLastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, "onInterceptTouchEvent: move");
                if (Math.abs(interceptLastX - x) > Math.abs(interceptLastY - y)) {
                    intercept = true;
                } else {
                    intercept = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "onInterceptTouchEvent: up");
                break;
        }
        lastX = x;
        lastY = y;
        Log.i(TAG, "onInterceptTouchEvent: " + intercept);
        return intercept;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getRawX();
        Log.i(TAG, "onTouchEvent: x" + x);
        float y = event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "onTouchEvent: down");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, "onTouchEvent: move");
                scrollBy((int) -(x - lastX), 0);
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "onTouchEvent: up");
                break;
        }
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int measureWidth = 0;
        int measureHeight = 0;
        int childCount = getChildCount();
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        Log.i(TAG, "onMeasure: widthSize:" + widthSize + " heightSize:" + heightSize);

        if (childCount == 0) {
            setMeasuredDimension(0, 0);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            measureWidth = getChildAt(0).getMeasuredWidth() * childCount;
            measureHeight = heightSize;
            setMeasuredDimension(measureWidth, measureHeight);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            measureWidth = widthSize;
            measureHeight = getChildAt(0).getMeasuredHeight();
            setMeasuredDimension(measureWidth, measureHeight);
        } else {
            measureWidth = getChildAt(0).getMeasuredWidth() * childCount;
            measureHeight = getChildAt(0).getMeasuredHeight();
            setMeasuredDimension(measureWidth, measureHeight);
            Log.i(TAG, "onMeasure: measureWidth measureHeight" + measureWidth + " " + measureHeight);
        }

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.layout(left, 0, left + child.getMeasuredWidth(), child.getMeasuredHeight());
            left += child.getMeasuredWidth();
        }
    }
}
